﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FrmSlopeIndependent : System.Windows.Forms.Form
	{
		public event FrmSlopeIndependent.SlopeIndependentDelegate SlopeIndependentEvent;

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public FrmSlopeIndependent(YJKWindowHandle HWndRevit)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{922B5986-4E02-4091-82F3-5109662615EF}", null);
			this.hWndRevit = HWndRevit;
			this.init();
		}

		private void init()
		{
			this.InitCboSlope();
			this.ReadDafaultData();
			this.SetPictureShow();
		}

		private void InitCboSlope()
		{
			this.cboSlope.Items.Add("读图");
			this.cboSlope.Items.Add("0.002");
			this.cboSlope.Items.Add("0.003");
			this.cboSlope.Items.Add("0.004");
			this.cboSlope.Items.Add("0.005");
			this.cboSlope.Items.Add("0.006");
			this.cboSlope.Items.Add("0.007");
		}

		private void rbnInLine_CheckedChanged(object sender, EventArgs e)
		{
			this.SetPictureShow();
		}

		private void FrmSlopeIndependent_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.WriteDafaultData();
			this.SimulationPressESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FrmSlopeIndependent.keybd_event((byte)bVk, 0, dwFlags, 0);
			FrmSlopeIndependent.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void btnSetting_Click(object sender, EventArgs e)
		{
			FormTotalMarkSetting formTotalMarkSetting = new FormTotalMarkSetting(ShowMarkType.Slope);
			formTotalMarkSetting.beEnableSlope = this.rbnStream.Checked;
			formTotalMarkSetting.ShowDialog();
			if (formTotalMarkSetting.DialogResult == DialogResult.OK)
			{
				this.setting = new UserSetting();
				this.markSetting = this.setting.GetSlopeMarkSetting();
				this.SetPictureShow();
			}
		}

		private void rbnStream_CheckedChanged(object sender, EventArgs e)
		{
			this.cboSlope.Enabled = !((RadioButton)sender).Checked;
			this.SetPictureShow();
		}

		private void cboSlope_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SetPictureShow();
		}

		private void FrmSlopeIndependent_Load(object sender, EventArgs e)
		{
			new YJKProcessTools().ShowInTaskbarChange(this);
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.KeyPreview = true;
		}

		private void FrmSlopeIndependent_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void SetPictureShow()
		{
			if (this.markSetting == null)
			{
				return;
			}
			Dictionary<string, Image> dictionary = new Dictionary<string, Image>();
			dictionary.Add("SlopeOutLineAll", YArchitech.HVAC.Properties.Resources.SlopeOutLineAll);
			dictionary.Add("SlopeOutLineHalf", YArchitech.HVAC.Properties.Resources.SlopeOutLineHalf);
			dictionary.Add("SlopeInLineHalf", YArchitech.HVAC.Properties.Resources.SlopeInLineHalf);
			dictionary.Add("SlopeInLineAll", YArchitech.HVAC.Properties.Resources.SlopeInLineAll);
			dictionary.Add("StreamOutLineAll", YArchitech.HVAC.Properties.Resources.StreamOutLineAll);
			dictionary.Add("StreamOutLineHalf", YArchitech.HVAC.Properties.Resources.StreamOutLineHalf);
			dictionary.Add("StreamInLineHalf", YArchitech.HVAC.Properties.Resources.StreamInLineHalf);
			dictionary.Add("StreamInLineAll", YArchitech.HVAC.Properties.Resources.StreamInLineAll);
			string text = "";
			if (this.rbnStream.Checked)
			{
				text += "Stream";
			}
			else
			{
				text += "Slope";
			}
			if (this.rbnOutLine.Checked)
			{
				text += "OutLine";
			}
			else
			{
				text += "InLine";
			}
			if (this.markSetting.Style == "全箭头")
			{
				text += "All";
			}
			else if (this.markSetting.Style == "半箭头")
			{
				text += "Half";
			}
			if (dictionary.ContainsKey(text))
			{
				this.pbxShow.Image = dictionary[text];
			}
			if (this.SlopeIndependentEvent != null)
			{
				EventArgs e = new EventArgs();
				this.SlopeIndependentEvent(text, e);
			}
			FrmSlopeIndependent.SendMessage(this.hWndRevit.Handle, this.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero);
		}

		private void WriteDafaultData()
		{
			this.setting = new UserSetting();
			SlopeIndependentData independent = new SlopeIndependentData
			{
				Solpe = ((this.cboSlope.SelectedItem == null) ? "" : this.cboSlope.SelectedItem.ToString()),
				IsOnline = this.rbnOutLine.Checked,
				IsStream = this.rbnStream.Checked
			};
			this.setting.WriteSlopeIndependentSetting(independent);
		}

		private void ReadDafaultData()
		{
			this.setting = new UserSetting();
			SlopeIndependentData slopeIndependentData = this.setting.ReadSlopeIndependentSetting();
			if (slopeIndependentData.Solpe == "")
			{
				this.cboSlope.SelectedIndex = 0;
			}
			else
			{
				this.cboSlope.SelectedItem = slopeIndependentData.Solpe;
			}
			this.rbnOutLine.Checked = slopeIndependentData.IsOnline;
			this.rbnInLine.Checked = !slopeIndependentData.IsOnline;
			this.rbnStream.Checked = slopeIndependentData.IsStream;
			this.frm.Checked = !slopeIndependentData.IsStream;
			this.markSetting = this.setting.GetSlopeMarkSetting();
			this.commFont = this.setting.GetFontSetting();
		}

		private void FrmSlopeIndependent_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private UserSetting setting;

		public SlopeMarkSet markSetting;

		private YJKWindowHandle hWndRevit;

		public CommonFont commFont;

		private int WM_SETFOCUS = 7;

		private ControlInfoManager m_thisManger;

		public delegate void SlopeIndependentDelegate(object sender, EventArgs e);
	}
}
